package com.john.data.source.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

/**
 * 多数据源config
 */
@Configuration
@PropertySource(value = {"classpath:application-dataSource.properties"})
public class DataSourceConfig {

    /**
     * 多数据源绑定 @ConfigurationProperties(prefix = "db")读取db.xxx开头的 如
     * db.username=xxx
     * @return
     */
    @Bean("oracleMyJohnDataSource")
    @ConfigurationProperties(prefix = "db")
    public DruidDataSource druidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }

    /**
     * 在注入的地方添加 @Qualifier("oracleMyJohnJdbcTemplate") 如下
 *     @Qualifier("oracleMyJohnJdbcTemplate")
 *     private NamedParameterJdbcTemplate oracleMyJohnJdbcTemplate;
     * @param dataSource
     * @return
     */
    @Bean(name = "oracleMyJohnJdbcTemplate")
    public NamedParameterJdbcTemplate oracleMyJohnJdbcTemplate(@Qualifier("oracleMyJohnDataSource") DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }


}
